/**
 *   Joomdiet - components/assets/javascript/dietcalibrator.js
 *   Author: Juan Dapena Paz (juan@bittingbits.com)
 *   Copyright (C) 2010 Bitting Bits S.L.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

window.addEvent('domready', function() {
    var selectedvalue = 0;
    var selectedtable = $$('#breakfast-div .content');
    var items = new Array();
    var ids = new Array();
    var food_ids = new Array();
    
    $$('.slider').set('slide', {duration: '500'});
    $$('.slider').slide('hide');
    $('breakfast-div').slide('show');
    
    $$('.daypartitem').addEvent('click', function() {
        $$('.daypartitem').removeClass('selected');
        this.addClass('selected');
        $$('.slider').slide('out');
        $(this.get('id')+'-div').slide('in');
        selectedtable = $$('#'+this.get('id')+'-div'+' .content');
    });
    
    $$('.tablerow').addEvent('click', function() {
        selectedvalue = this.get('id').substring(7);
        $$('.tablerow').removeClass('selected');
        this.addClass('selected');
//        selectedtable = this;
    });
    
    /*$$('.tablerow').each(function(drag) {
        new Drag.Move(drag, {
            droppables: $$('.slider')
        });
        
        drag.addEvent('emptydrop', function() {
            this.setStyle('background-color', '#ccc');
        });
    });*/
    
    $('addrecipe').addEvent('click', function() {
        $('task').set('value', 'loadfooditem');
        $('format').set('value', 'raw');
        $('recipeid').set('value', selectedvalue);
        $('itemtype').set('value', $('type'+selectedvalue).get('value'));
        $('form1').send({
            onComplete: function (response, responseXML) 
            {
                selectedtable.setHTML(selectedtable.getHTML()+response);
                
                var item = $('removerecipeitem'+selectedvalue);
                items.push(item);
                if ($('itemtype').get('value') == 1)
                    ids.push(selectedvalue);
                else food_ids.push(selectedvalue);
                regeninputfield();
                $$('.removerecipeitem').addEvent('click', 
                                                 removerecipeitemhandler);
                $('format').set('value', 'html');
                $('task').set('value', 'loadinfotable');
            }
        });
    });
    
    /*$('viewcalc').addEvent('click', function() {
        $('task').set('value', 'viewcalc');
        $('form1').send(
            onComplete: function (response, responseXML)
            {
                $('menucontentarea').setHTML('groar');
            }
        );
    });*/
    
    function regeninputfield() {
        var ids_string = '';
        for (i = 0; i < ids.length; i++) {
            if (i < (ids.length - 1)) 
                ids_string += ids[i] + ' ';
            else ids_string += ids[i];
        }
        $('recipes').set('value', ids_string);
        var foods_string = '';
        for (i = 0; i < food_ids.length; i++) {
            if (i < (food_ids.length - 1)) 
                foods_string += food_ids[i] + ' ';
            else foods_string += food_ids[i];
        }
        $('foods').set('value', foods_string);
    }
    
    function removerecipeitemhandler()
    {
        var toremoveid = this.get('id').substring(16);
        var toremovediv = $('recipeitem'+toremoveid);
        var toremoveindex;
        
        for (i = 0; i < ids.length; i++) {
            if (ids[i] == toremoveid) {
                toremoveindex = i;
            }
        }
        ids.splice(i);
        regeninputfield();
        
        /*toremovediv.fade('out').chain(function() { 
            toremovediv.destroy();
        });*/
        var fadeFx = new Fx.Tween(toremovediv, {
            duration: 500,
            onComplete: function() { toremovediv.destroy(); }
        }).start('opacity', 0);
    }
});
